package com.elbehiry.delish.presentation

import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import app.delish.compose.theme.BottomSheetShape
import com.elbehiry.delish.presentation.navigation.AppNavigation
import com.elbehiry.delish.presentation.navigation.BottomBar
import com.elbehiry.delish.presentation.navigation.Navigate
import com.elbehiry.delish.presentation.vm.MainViewModel
import com.elbehiry.delish.presentation.vm.ViewEvent
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import com.google.accompanist.navigation.material.ModalBottomSheetLayout
import com.google.accompanist.navigation.material.rememberBottomSheetNavigator

@Composable
internal fun MainScreen(
    mainViewModel: MainViewModel,  // 主界面视图模型
    onLauncherFinished: () -> Unit  // 启动完成回调
) {
    // 创建底部弹窗导航控制器
    val bottomSheetNavigator = rememberBottomSheetNavigator()
    // 创建带有底部弹窗支持的动画导航控制器
    val navController = rememberAnimatedNavController(bottomSheetNavigator)
    // 保持底部导航栏状态的可见性（使用rememberSaveable保持配置更改时的状态）
    val bottomBarState = rememberSaveable { mutableStateOf(false) }

    // 收集视图模型的状态流
    val viewState by mainViewModel.states.collectAsState()

    // 检查引导页显示状态是否已获取
    if (viewState.isOnBoardingShown == null) {
        // 如果状态为null，请求视图模型获取引导页状态
        mainViewModel.processEvent(ViewEvent.OnBoardingStatus)
    } else {
        // 根据引导页状态决定起始路由
        val screen = if (viewState.isOnBoardingShown == true) {
            Navigate.Screen.Main.route  // 已显示过引导页，进入主界面
        } else {
            Navigate.Screen.OnBoardingWelcome.route  // 未显示过引导页，进入引导页
        }

        // 底部弹窗布局（用于显示底部导航栏和导航内容）
        ModalBottomSheetLayout(
            bottomSheetNavigator = bottomSheetNavigator,  // 连接底部弹窗导航器
            sheetShape = BottomSheetShape,                // 设置弹窗形状
            sheetBackgroundColor = Color.Transparent      // 透明背景
        ) {
            // 主界面脚手架布局（包含应用栏、底部导航等标准元素）
            Scaffold(
                backgroundColor = MaterialTheme.colors.surface,  // 背景色使用主题的表面色
                bottomBar = {
                    // 底部导航栏（可见性由bottomBarState控制）
                    BottomBar(navController, bottomBarState)
                }
            ) { innerPadding ->  // Scaffold提供的内边距参数
                // 使用BoxWithConstraints获取父布局的尺寸限制
                BoxWithConstraints(
                    modifier = Modifier.fillMaxSize()
                ) {
                    // 应用主导航组件
                    AppNavigation(
                        navController = navController,     // 主导航控制器
                        startDestination = screen,         // 根据条件计算的起始路由
                        width = constraints.maxWidth / 2,  // 使用最大宽度的一半（可能是布局约束）
                        bottomBarPadding = innerPadding,   // 传递Scaffold的内边距
                        bottomBarState = bottomBarState    // 底部导航栏状态控制
                    )
                }
            }
        }

        // 启动流程已完成，通知调用方
        onLauncherFinished()
    }
}
